import exceptions.RentalException;
import rental.Customer;
import rental.DVD;
import rental.Rent;
import junit.framework.TestCase;


public class TestCustomer extends TestCase {
	
	Customer c;
	Rent r;
	DVD dvd;
	
	protected void setUp() throws Exception {
		c = new Customer("John");
		r = new Rent(new DVD("title", DVD.REGULAR), 2);
	}

	public void testCustomer() {
		Customer c = new Customer("John");
		assertNotNull(c);
		assertEquals("John", c.getName());
	}

	public void testAddRent() throws RentalException {
		DVD dvd1 = new DVD("The Exorcist", DVD.REGULAR); 
		Rent r1 = new Rent(dvd1, 2);
		
		DVD dvd2 = new DVD("Jurassic Park III", DVD.NEW_MOVIE); 
		Rent r2 = new Rent(dvd2, 2);
		
		assertEquals(0, c.getRentedMovies().size());
		try {
			c.addRent(r1);
			assertEquals(1, c.getRentedMovies().size());
			c.addRent(r2);
			assertEquals(2, c.getRentedMovies().size());
			c.addRent(r1);
			assertEquals(3, c.getRentedMovies().size());
		} catch (RentalException e) {
			fail();
		}
	}
	
	public void testAddRent2() {
		Rent r = null;
		
		assertEquals(0, c.getRentedMovies().size());
		try {
			c.addRent(r);
			fail("An excpetion should have been throwed.");
		} catch (RentalException e) {
		}
		
	}

	public void testAddRent3() throws RentalException {
		for (int i = 1; i < 1001; i++) {
			DVD dvd = new DVD("The Exorcist", DVD.REGULAR); 
			Rent r = new Rent(dvd, 2);
			c.addRent(r);
			assertEquals(i, c.getRentedMovies().size());
		}
				
		assertEquals(1000, c.getRentedMovies().size());		
	}
	
	public void testGetTotalValue() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.REGULAR), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.REGULAR), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.REGULAR), 10);
		
		c.addRent(r1);
		assertEquals(2.0, c.getTotalValue());
		c.addRent(r2);
		assertEquals(4.0, c.getTotalValue());
		c.addRent(r3);
		assertEquals(18.0, c.getTotalValue());
	}
	
	public void testGetTotalValue2() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.FOR_KIDS), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.FOR_KIDS), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.FOR_KIDS), 10);
		
		c.addRent(r1);
		assertEquals(1.5, c.getTotalValue());
		c.addRent(r2);
		assertEquals(3.0, c.getTotalValue());
		c.addRent(r3);
		assertEquals(15.0, c.getTotalValue());
	}
	
	public void testGetTotalValue3() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.NEW_MOVIE), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.NEW_MOVIE), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.NEW_MOVIE), 10);
		
		c.addRent(r1);
		assertEquals(6.0, c.getTotalValue());
		c.addRent(r2);
		assertEquals(9.0, c.getTotalValue());
		c.addRent(r3);
		assertEquals(39.0, c.getTotalValue());
	}
	
	public void testGetTotalValue4() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.REGULAR), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.FOR_KIDS), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.NEW_MOVIE), 10);
		
		c.addRent(r1);
		assertEquals(2.0, c.getTotalValue());
		c.addRent(r2);
		assertEquals(3.5, c.getTotalValue());
		c.addRent(r3);
		assertEquals(33.5, c.getTotalValue());
	}
	
	public void testGetTotalValue5() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.FOR_KIDS), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.NEW_MOVIE), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.REGULAR), 10);
		
		c.addRent(r1);
		assertEquals(1.5, c.getTotalValue());
		c.addRent(r2);
		assertEquals(4.5, c.getTotalValue());
		c.addRent(r3);
		assertEquals(18.5, c.getTotalValue());
	}
	
	public void testGetProgramPoints1() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.REGULAR), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.REGULAR), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.REGULAR), 10);
		
		c.addRent(r1);
		assertEquals(1, c.getProgramPoints());
		c.addRent(r2);
		assertEquals(2, c.getProgramPoints());
		c.addRent(r3);
		assertEquals(3, c.getProgramPoints());
	}
	
	public void testGetProgramPoints2() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.FOR_KIDS), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.FOR_KIDS), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.FOR_KIDS), 10);
		
		c.addRent(r1);
		assertEquals(1, c.getProgramPoints());
		c.addRent(r2);
		assertEquals(2, c.getProgramPoints());
		c.addRent(r3);
		assertEquals(3, c.getProgramPoints());
	}
	
	public void testGetProgramPoints3() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.NEW_MOVIE), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.NEW_MOVIE), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.NEW_MOVIE), 10);
		
		c.addRent(r1);
		assertEquals(2, c.getProgramPoints());
		c.addRent(r2);
		assertEquals(3, c.getProgramPoints());
		c.addRent(r3);
		assertEquals(5, c.getProgramPoints());
	}
	
	public void testGetProgramPoints4() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.REGULAR), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.FOR_KIDS), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.NEW_MOVIE), 10);
		
		c.addRent(r1);
		assertEquals(1, c.getProgramPoints());
		c.addRent(r2);
		assertEquals(2, c.getProgramPoints());
		c.addRent(r3);
		assertEquals(4, c.getProgramPoints());
	}
	
	public void testGetProgramPoints5() throws RentalException{
		Rent r1 = new Rent(new DVD("title", DVD.FOR_KIDS), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.NEW_MOVIE), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.REGULAR), 10);
		
		c.addRent(r1);
		assertEquals(1, c.getProgramPoints());
		c.addRent(r2);
		assertEquals(2, c.getProgramPoints());
		c.addRent(r3);
		assertEquals(3, c.getProgramPoints());
	}
	
	public void testExtract1() throws RentalException {
		Rent r1 = new Rent(new DVD("title1", DVD.FOR_KIDS), 2);
		Rent r2 = new Rent(new DVD("title2", DVD.NEW_MOVIE), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.REGULAR), 10);
		
		c.addRent(r1);
		c.addRent(r2);
		c.addRent(r3);
		assertEquals(18.5, c.getTotalValue());
		
		final String endOfLine = System.getProperty("line.separator");
		String extract = "History of John's rents:"+ endOfLine+
		"\t" + "title1" + "\t" + "1.5" + "\t" + "Classification: Movie recommended for all ages."+endOfLine+
		"\t" + "title2" + "\t" + "3.0" + "\t" + "Classification: This is a new movie. Please check the classification in the back cover."+endOfLine+
		"\t" + "title3" + "\t" + "14.0" + "\t" + "Classification: Please check the classification in the back cover."+endOfLine+
		"Total Price: 18.5"+endOfLine+
		"Program points 3 points";
		assertEquals(extract, c.extract());
	}
	
	public void testExtract2() throws RentalException {
		Rent r1 = new Rent(new DVD("title1", DVD.FOR_KIDS), 10);
		Rent r2 = new Rent(new DVD("title2", DVD.NEW_MOVIE), 1);
		Rent r3 = new Rent(new DVD("title3", DVD.REGULAR), 10);
		
		c.addRent(r1);
		c.addRent(r2);
		c.addRent(r3);
		
		final String endOfLine = System.getProperty("line.separator");
		String extract = "History of John's rents:"+ endOfLine+
		"\t" + "title1" + "\t" + "12.0" + "\t" + "Classification: Movie recommended for all ages."+endOfLine+
		"\t" + "title2" + "\t" + "3.0" + "\t" + "Classification: This is a new movie. Please check the classification in the back cover."+endOfLine+
		"\t" + "title3" + "\t" + "14.0" + "\t" + "Classification: Please check the classification in the back cover."+endOfLine+
		"Total Price: 29.0"+endOfLine+
		"Program points 3 points";
		assertEquals(extract, c.extract());
	}

	
	public void testExtract3() throws RentalException {
		Rent r1 = new Rent(new DVD("title1", DVD.FOR_KIDS), 10);
		Rent r2 = new Rent(new DVD("title2", DVD.NEW_MOVIE), 10);
		Rent r3 = new Rent(new DVD("title3", DVD.REGULAR), 10);
		
		c.addRent(r1);
		c.addRent(r2);
		c.addRent(r3);
		
		final String endOfLine = System.getProperty("line.separator");
		String extract = "History of John's rents:"+ endOfLine+
		"\t" + "title1" + "\t" + "12.0" + "\t" + "Classification: Movie recommended for all ages."+endOfLine+
		"\t" + "title2" + "\t" + "30.0" + "\t" + "Classification: This is a new movie. Please check the classification in the back cover."+endOfLine+
		"\t" + "title3" + "\t" + "14.0" + "\t" + "Classification: Please check the classification in the back cover."+endOfLine+
		"Total Price: 56.0"+endOfLine+
		"Program points 4 points";
		assertEquals(extract, c.extract());
	}
	
	
	public void testExtract4() throws RentalException {
		Rent r ;
		
		final String endOfLine = System.getProperty("line.separator");
		String str = "History of John's rents:"+ endOfLine;
		double total = 0.0;
		
		for (int i = 0; i < 1000; i++) {
			r = new Rent(new DVD("title1", DVD.FOR_KIDS), 1);
			c.addRent(r);
			str+= "\t" + "title1" + "\t" + "1.5" + "\t" + "Classification: Movie recommended for all ages."+endOfLine;
			total+= 1.5;
		}
		
		str+= "Total Price: " + total+ endOfLine+
		"Program points 1000 points";
		assertEquals(str, c.extract());
	}
	
}
